/**
 * 
 */
package com.acc.softmgr.common.genertor;

import java.util.Date;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;
import com.acc.softmgr.common.genertor.gen.GenerateController;
import com.acc.softmgr.common.genertor.gen.GenerateDao;
import com.acc.softmgr.common.genertor.gen.GenerateEntity;
import com.acc.softmgr.common.genertor.gen.GenerateEntityExample;
import com.acc.softmgr.common.genertor.gen.GenerateHTML;
import com.acc.softmgr.common.genertor.gen.GenerateJS;
import com.acc.softmgr.common.genertor.gen.GenerateMapperXML;
import com.acc.softmgr.common.genertor.gen.GenerateService;
import com.acc.softmgr.common.genertor.gen.GenerateServiceImpl;
import com.acc.softmgr.common.genertor.utils.CodeResourceUtil;
import com.acc.softmgr.common.genertor.utils.CommUtil;
import com.acc.softmgr.common.genertor.utils.CreateBeanUtil;

/**
 * 
 * @content 代码生成工厂
 * 
 * @date 2017年6月3日
 * 
 * @author OprCalf
 * 
 */
public class CodeGenerateFactory {

	private static final Log log = LogFactory.getLog(CodeGenerateFactory.class);

	private static String url = CodeResourceUtil.URL;

	private static String username = CodeResourceUtil.USERNAME;

	private static String passWord = CodeResourceUtil.PASSWORD;

	private static String projectPath = CommUtil.getProjectPath();

	public static void codeGenerate(String tableName) {

		CreateBeanUtil createBean = new CreateBeanUtil();

		createBean.setMysqlInfo(url, username, passWord);

		String className = CommUtil.formatTableNameToClassName(tableName);

		String lowerName = className.substring(0, 1).toLowerCase() + className.substring(1, className.length());

		String srcPath = projectPath + CodeResourceUtil.source_root_package + "\\";

		String pckPath = srcPath + CodeResourceUtil.bussiPackageUrl + "\\";

		String baseHTMLPath = projectPath + CodeResourceUtil.source_root_html + "\\";
		
		String baseJSPath = projectPath + CodeResourceUtil.source_root_js + "\\";

		String beanPath = "model\\" + className + ".java";

		String examplePath = "model\\" + className + "Example.java";
		
		String mapperXMPPath = "model\\mapper\\" + className + "Mapper.xml";

		String daoPath = "dao\\" + "I" + className + "Dao.java";

		String servicePath = "service\\" + "I" + className + "Service.java";

		String serviceImplPath = "service\\impl\\" + className + "ServiceImpl.java";

		String controllerPath = "controller\\" + className + "Controller.java";

		String htmlPath = lowerName + "\\" + lowerName + ".html";
		
		String jsPath = lowerName + "\\" + lowerName + ".js";

		VelocityContext context = new VelocityContext();
		context.put("basePackage", CodeResourceUtil.basePackage);
		context.put("createDate", new Date());
		context.put("className", className);
		context.put("lowerName", lowerName);
		context.put("tableName", tableName);
		try {
			context.put("feilds", createBean.getBeanFeilds(tableName));
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			Map<String, Object> sqlMap = createBean.getAutoCreateSql(tableName);
			context.put("columnDatas", createBean.getColumnDatas(tableName));
			context.put("SQL", sqlMap);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		try {
			GenerateEntity.genEntity(tableName, pckPath, beanPath);
			GenerateEntityExample.genEntityExampl(tableName, pckPath, examplePath);
			GenerateDao.genDao(tableName, pckPath, daoPath);
			GenerateMapperXML mapperXML = new GenerateMapperXML();
			mapperXML.genMapperXML(tableName, pckPath, mapperXMPPath);
			GenerateService.genService(tableName, pckPath, servicePath);
			GenerateServiceImpl.genServiceImpl(tableName, pckPath, serviceImplPath);
			GenerateController controller = new GenerateController();
			controller.genController(tableName, pckPath, controllerPath);
			GenerateHTML.genHTML(tableName, baseHTMLPath, htmlPath);
			GenerateJS.genJS(tableName, baseJSPath, jsPath);
		} catch (Exception e) {
			log.error(e);
		}
		log.info("----------------------------\u4EE3\u7801\u751F\u6210\u5B8C\u6BD5---------------------------");
	}

	public static void main(String[] args) {
		String tableName = "test_table";
		CodeGenerateFactory.codeGenerate(tableName);
	}

}
